package com.ruoyi.finance.livestreaming.controller;


import com.github.pagehelper.util.StringUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.finance.livestreaming.service.FiDigitalLivestreamingUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RestController
@RequestMapping("/finance/livestreamingUser")
public class FiDigitalLivestreamingUserController {

    @Autowired
    private FiDigitalLivestreamingUserService fiDigitalLivestreamingUserService;

    @PreAuthorize("@ss.hasPermi('finance:livestreaming:balanceWithdrawal')")
    @Log(title = "数字直播-钱包余额提现", businessType = BusinessType.OTHER)
    @PostMapping("/balanceWithdrawal")
    public AjaxResult balanceWithdrawal(String paymentPassword, BigDecimal amount, String userId, String remark){
        if (StringUtil.isEmpty(userId)|| StringUtil.isEmpty(paymentPassword) || amount == null || amount.compareTo(BigDecimal.ZERO) == 0){
            return AjaxResult.error();
        }
        return fiDigitalLivestreamingUserService.balanceWithdrawal(userId, paymentPassword, amount, remark);
    }


    @PreAuthorize("@ss.hasPermi('finance:livestreaming:rewardWithGifts')")
    @Log(title = "数字直播-礼物打赏", businessType = BusinessType.OTHER)
    @PostMapping("/rewardWithGifts")
    public AjaxResult rewardWithGifts(Long userId, Long rewardedUser, Long giftId, BigDecimal price, BigDecimal count){
        if (userId == null || rewardedUser == null || giftId == null || price == null || count == null || price.compareTo(BigDecimal.ZERO) == 0 || count.compareTo(BigDecimal.ZERO) == 0) {
            return AjaxResult.error();
        }
        return fiDigitalLivestreamingUserService.rewardWithGifts(userId, rewardedUser, giftId, price, count);
    }


    @Log(title = "数字直播-vip变更记录查询", businessType = BusinessType.OTHER)
    @PostMapping("/vipUpgradeRecord")
    public AjaxResult vipUpgradeRecord(Long userId){
        if (userId == null || userId == 0L){
            return null;
        }
        return fiDigitalLivestreamingUserService.vipUpgradeRecord(userId);
    }


    @Log(title = "数字直播-抵扣金额变化记录查询", businessType = BusinessType.OTHER)
    @PostMapping("/deductionAmountRecord")
    public AjaxResult deductionAmountRecord(Long userId){
        if (userId == null || userId == 0L){
            return null;
        }
        return fiDigitalLivestreamingUserService.deductionAmountRecord(userId);
    }


    @PreAuthorize("@ss.hasPermi('finance:livestreaming:updateUserVip')")
    @Log(title = "数字直播-修改用户VIP等级", businessType = BusinessType.UPDATE)
    @PostMapping("/updateUserVip")
    public AjaxResult updateUserVip(Long userId,Long vipId){
        if (vipId == null || vipId == 0L ||userId == null || userId == 0L){
            return AjaxResult.error("代理等级不能为空");
        }
        return fiDigitalLivestreamingUserService.updateUserVip(userId, vipId);
    }


    @PreAuthorize("@ss.hasPermi('finance:livestreaming:updateDeduction')")
    @Log(title = "数字直播-抵扣金额还款", businessType = BusinessType.UPDATE)
    @PostMapping("/updateDeduction")
    public AjaxResult updateDeduction(String userId, BigDecimal amount){
        if (StringUtil.isEmpty(userId)){
            return AjaxResult.error();
        }

        if (amount.compareTo(BigDecimal.ZERO) == 0){
            return AjaxResult.success();
        }
        return fiDigitalLivestreamingUserService.updateDeduction(userId, amount);
    }



    // 查询礼物列表
    @GetMapping("/getSzGift")
    public AjaxResult getSzGift(){
        return fiDigitalLivestreamingUserService.getSzGift();
    }

    // 查询主播列表 （已开通直播的用户）
    @GetMapping("/getSzanchor")
    public AjaxResult getSzanchor(){
        return fiDigitalLivestreamingUserService.getSzanchor();
    }


}
